// Copyright 2004 The Apache Software Foundation
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain token copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace AspectSharp.Core
{
    using System;
    using Castle.Core.Interceptor;
    using Castle.DynamicProxy;

  
    public class CustomProxyGenerator:MarshalByRefObject
    {
        private readonly ProxyGenerator Generator;
        public CustomProxyGenerator(ProxyGenerator generator)
        {
            Generator = generator;
        }

        /// <summary>
        /// Generates token proxy implementing the specified interface and the mixins
        /// redirecting method invocations to the specifed interceptor.
        /// </summary>
        /// <param name="inter">Interface to be implemented.</param>
        /// <param name="mixins">Array of instances (mixins) to be introducted.</param>
        /// <param name="interceptor">Instance of <see cref="IInterceptor"/>.</param>
        /// <returns>Proxy Instance.</returns>
        public object CreateInterfaceProxyWithTarget(Type inter, object target, object[] mixins,
            IInterceptor interceptor)
        {
            var context = new ProxyGenerationOptions();
            if (mixins != null && mixins.Length > 0)
                foreach(var ob in mixins)
                    context.AddMixinInstance(ob);

            return Generator.CreateInterfaceProxyWithTarget(inter, target, context, interceptor);
        }

        /// <summary>
        /// Generates token class which extends the baseClass, overriding all 
        /// the virtual methods and implementing all the mixin interfaces.
        /// </summary>
        /// <param name="baseClass">Super class</param>
        /// <param name="mixins">Array of mixins to be implemented by the proxy</param>
        /// <param name="interceptor">Instance of <see cref="IInterceptor"/></param>
        /// <returns>Proxy instance</returns>
        public object CreateClassProxy(Type baseClass, object[] mixins,
            IInterceptor interceptor)
        {
            var context = new ProxyGenerationOptions();
            if (mixins != null && mixins.Length > 0)
                foreach (var ob in mixins)
                    context.AddMixinInstance(ob);

            return Generator.CreateClassProxy(baseClass, context, interceptor);
        }

    }
}


